package com.chixing.service.impl;

import com.chixing.mapper.*;
import com.chixing.pojo.*;
import com.chixing.service.CarDetailService;
import com.chixing.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CarDetailServiceImpl implements CarDetailService {

    public static final String COLLECTIONNAME_REVIEW = "review";

    @Autowired
    private CarMapper carMapper;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ReviewMapper reviewMapper;

    @Autowired
    private CarImgMapper carImgMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SupportMapper  supportMapper;


    @Override
    public ServerResult getById(Integer carId) {

        Car car = carMapper.selectByPrimaryKey(carId);
        CarImgExample carImgExample = new CarImgExample();
        carImgExample.createCriteria().andCarIdEqualTo(carId);
        List<CarImg> carImgList= carImgMapper.selectByExample(carImgExample);
        Integer shopId = car.getShopId();
        ReviewExample reviewExample = new ReviewExample();
        reviewExample.createCriteria().andShopIdEqualTo(shopId);
        List<Review> reviewList = reviewMapper.selectByExampleWithBLOBs(reviewExample);
        System.out.println("列表："+reviewList);
        Map<Review, Member> reviewMemberMap = new HashMap<>();
        Member member;
        if(reviewList.size()>0) {
            for (Review review : reviewList) {
                System.out.println("1:" + review);
                System.out.println(review.getMemberId());
                member = memberMapper.selectByPrimaryKey(review.getMemberId());
                System.out.println(member);
                reviewMemberMap.put(review, member);
            }
        }
        System.out.println(reviewMemberMap);

        Map<Integer,List<Integer>> Review_member = new HashMap<>();
        if(reviewList.size()>0) {
            for (Review review : reviewList) {
                System.out.println("1:" + review);
                System.out.println(review.getReviewId());
                SupportExample example = new SupportExample();
                example.createCriteria().andReviewIdEqualTo(review.getReviewId()).andSupportStatusEqualTo(1);
                List<Support> supportList = supportMapper.selectByExample(example);
                List<Integer> supportId = new ArrayList<>();
                for(Support support : supportList){
                    supportId.add(support.getMemberId());
                }
                Review_member.put(review.getReviewId(),supportId);

            }
            System.out.println("评论id+用户id"+Review_member);
        }

        Map<String,Integer> custLikeRecordMap =redisTemplate.opsForHash().entries("save_praise_record");
        System.out.println(custLikeRecordMap);
        Set<Map.Entry<String,Integer>> set = custLikeRecordMap.entrySet();
        for(Map.Entry<String,Integer> entry:set){ //10001::50035=1,
            Integer memberId =  Integer.valueOf( entry.getKey().split("::")[0]); //10001
            Integer reviewId =  Integer.valueOf( entry.getKey().split("::")[1]);  //50035
            Integer praiseStatus = entry.getValue(); //1

            if(praiseStatus==1){
                if(Review_member.containsKey(reviewId)){
                    List<Integer> list = Review_member.get(reviewId);
                    list.add(memberId);
                    Review_member.put(reviewId,list);
                }else {
                    List<Integer> list =new ArrayList<>();
                    list.add(memberId);
                    Review_member.put(reviewId,list);
                }
            }

        }

        for (Map.Entry<Integer, List<Integer>> entry : Review_member.entrySet()) {
            Integer mapKey = entry.getKey();
            List<Integer> mapValue = entry.getValue();
            mapValue.add(0);
            Review_member.put(mapKey,mapValue);
        }
        System.out.println("评论id+用户id"+Review_member);


        CarDetailVO vo = new CarDetailVO(car,carImgList,reviewMemberMap,Review_member);

        System.out.println("=========ShopDetailVO===========\n" + vo);

        if (vo != null)
            return ServerResult.getSussess(vo);
        return ServerResult.getFail(null);

    }
}
